package com.wondersgroup.framework.sample.component.tree.action;

import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.sample.component.data.TreeData;
import com.wondersgroup.framework.sample.component.tree.vo.CheckTreeVo;
import com.wondersgroup.framework.sample.component.tree.vo.FullTreeVo;
import com.wondersgroup.framework.sample.component.tree.vo.TreeVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;

public class TreeAction extends BaseAjaxAction
{
  private HashMap<CheckTreeVo, String> treeData;
  private boolean hasTrue;
  private List<FullTreeVo> fullTreeData;
  private HashMap<TreeVo, String> uncheckTreeData;

  public TreeAction()
  {
    this.treeData = new HashMap();

    this.hasTrue = false;

    this.fullTreeData = new ArrayList();

    this.uncheckTreeData = new HashMap();
  }

  public String query()
  {
    if (this.hasTrue != true)
      this.treeData = new TreeData(this.servletRequest).getTreeData();

    this.hasTrue = true;
    List result = new ArrayList();
    String parentId = this.servletRequest.getParameter("parentNodeId");

    String[] parentIds = (String[])this.treeData.values().toArray(new String[this.treeData.size()]);
    CheckTreeVo[] categories = (CheckTreeVo[])this.treeData.keySet().toArray(new CheckTreeVo[this.treeData.size()]);
    for (int i = 0; i < parentIds.length; ++i) {
      if (parentIds[i].equalsIgnoreCase(parentId))
        result.add(categories[i]);

    }

    String json = VOUtils.getJsonDataFromCollection(result);
    createJSonData(json);
    this.logger.info("树节点数据逐层加载");
    return "ajax";
  }

  public String uncheckTreequery()
  {
    if (this.hasTrue != true)
      this.uncheckTreeData = new TreeData(this.servletRequest).getUnCheckTreeData();

    this.hasTrue = true;
    List result = new ArrayList();
    String parentId = this.servletRequest.getParameter("parentNodeId");

    String[] parentIds = (String[])this.uncheckTreeData.values().toArray(new String[this.uncheckTreeData.size()]);
    TreeVo[] categories = (TreeVo[])this.uncheckTreeData.keySet().toArray(new TreeVo[this.uncheckTreeData.size()]);
    for (int i = 0; i < parentIds.length; ++i) {
      if (parentIds[i].equalsIgnoreCase(parentId))
        result.add(categories[i]);

    }

    String json = VOUtils.getJsonDataFromCollection(result);
    createJSonData(json);
    this.logger.info("树节点数据逐层加载");
    return "ajax";
  }

  public String fullTreeQuery() {
    this.fullTreeData = new TreeData(this.servletRequest).getFullTreeData();

    String json = VOUtils.getJsonDataFromCollection(this.fullTreeData);
    createJSonData(json);
    this.logger.info("树节点数据一次性加载");
    return "ajax";
  }
}